package servlet;

import entity.Lesson;
import service.LessonService;
import service.impl.LessonServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/lessonListServlet")
public class LessonListServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        String username = String.valueOf(session.getAttribute("username"));

        //1.调用LessonService完成查询
        LessonService service = new LessonServiceImpl();
        List<Lesson> lessons = null;
        try {
            lessons = service.findLessons(username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //2.将list存入request域
        request.setAttribute("lessons",lessons);
        for(Lesson l : lessons){
            System.out.println(l);
        }
        //3.转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
